<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <FormPanel ref="formPanel1" :list="formList1" :formType="type" @formChange="formChangeHandle1" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <FilterTable ref="table" height="auto" :isSelectColumn="false" :columns="columns" :dataSource="tableList" :onColumnsChange="columns => (this.columns = columns)" />

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <multiuse-button type="primary" :click="dataHandle.bind(this, 60901004)">保 存</multiuse-button>
      <multiuse-button type="primary" :click="dataHandle.bind(this, 60901005)">提 交</multiuse-button>
      <el-button @click="closeDrawer">关 闭</el-button>
    </div>

    <BaseDialog :visible.sync="isShowRcpt" :title="'收件人信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SearchRcpt :rowData="formData" @close="closeDialog" />
    </BaseDialog>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { getUser } from '@/assets/js/auth';
import { getRecipient, registration } from '@/api/dealerFinance/InvoiceManage/OldInvoiceReturnRegist';
import SearchRcpt from './SearchRcpt.vue';

export default {
  name: 'SonModel',
  mixins: [dictionary],

  props: ['type', 'rowData'],
  data() {
    this.BaseTable = null;
    this.saveData = null;
    this.saveData1 = null;

    return {
      formList: this.createdFrom(),
      formList1: this.createdFrom1(),
      columns: this.createdTable(),
      tableList: [],
      isCan: false,
      isShowRcpt: false,
      formData: {},
      backRecipientId: ''
    };
  },
  components: {
    SearchRcpt
  },
  mounted() {
    this.init();
    this.BaseTable = this.$refs.table.$refs.pageTable;

    console.log(this.rowData, '---奥利给---');
  },
  methods: {
    init() {
      this.formList.map(x => {
        let data = this.rowData[0];
        if (x.fieldName == 'djr' || x.fieldName == 'djrq' || x.fieldName == 'backMailNo') {
        } else if (x.fieldName == 'backCourierDate') {
          x.initialValue = data['backCourierDate'] ? data['backCourierDate'] : moment().format('YYYY-MM-DD');
        } else {
          x.initialValue = data[x.fieldName];
        }
      });
      this.tableList = this.rowData;
      this.getRecipient();
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async getRecipient() {
      const res = await getRecipient({
        powerType: this.rowData[0].powerType || '',
        buzinessType: this.rowData[0].buzinessType || ''
      });
      if (res.resultCode == 200) {
        this.formList1.map(x => {
          x.initialValue = res.data[x.fieldName];
        });
      }
    },
    searchFun() {
      this.isShowRcpt = true;
      this.formData = this.rowData;
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    formChangeHandle1(val) {
      this.saveData1 = val;
    },
    // 登记   寄件人ID、收件人ID、动力形式、业务类型、经销商代码、经销商名称、备注、发票选中的ID；
    async dataHandle(type) {
      this.$refs.formPanel.SUBMIT_FORM();
      this.$refs.formPanel1.SUBMIT_FORM();
      if (this.saveData) {
        let arrJson = [];

        this.rowData.map(x => {
          let json = {
            backCourierCompany: this.saveData.backCourierCompany,
            backCourierDate: this.saveData.backCourierDate,
            backCourierNo: this.saveData.backCourierNo,
            backRemark: this.saveData.backRemark,
            backMailNo: x.backMailNo || '',
            backRecipientId: this.backRecipientId || '',
            invoiceId: x.invoiceId,
            postType: type,
            powerType: x.powerType,
            buzinessType: x.buzinessType
          };
          arrJson.push(json);
        });
        let res = await registration(arrJson);

        if (res.resultCode == 200) {
          this.$notify.success({ title: '提示信息', message: '操作成功' });
          this.$emit('close', false, 'refresh');
        }
      }
    },
    closeDialog(val, data) {
      console.log(data, '---收件人信息---');
      this.backRecipientId = data[0].id;
      this.isShowRcpt = val;
      this.setFormData(data[0]);
    },
    setFormData(data) {
      for (let key in data) {
        this.formList1.map(x => {
          x.initialValue = data[x.fieldName];
        });
      }
    },
    closeDrawer() {
      this.$emit('close', false);
    },
    delHandle(row) {
      this.BaseTable.EXECUTE_DELETE([row]);
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '退回单号',
          fieldName: 'backMailNo',
          placeholder: '系统自动生成',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '登记人',
          fieldName: 'djr',
          placeholder: '',
          initialValue: getUser(),
          disabled: true
        },
        {
          type: 'DATE',
          label: '登记日期',
          fieldName: 'djrq',
          initialValue: moment().format('YYYY-MM-DD'),
          dateType: 'exactdate',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '快递公司',
          fieldName: 'backCourierCompany',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '快递单号',
          fieldName: 'backCourierNo',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '快递日期',
          fieldName: 'backCourierDate',
          initialValue: '',
          dateType: 'exactdate',
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'backRemark',
          placeholder: '请输入',
          selfCols: 3
        }
      ];
    },
    createdFrom1() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '收件人信息'
        },
        {
          type: 'INPUT',
          label: '姓名',
          fieldName: 'recipientName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '邮编',
          fieldName: 'recipientPostCode',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '电话',
          fieldName: 'recipientPhone',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '移动电话',
          fieldName: 'recipientMobile',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '省份',
          fieldName: 'provinceName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '所在市',
          fieldName: 'cityName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '县区',
          fieldName: 'districtName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '详细地址',
          fieldName: 'address',
          selfCols: 2,
          placeholder: '',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.searchFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'BREAK_SPACE',
          label: '发票列表'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 50,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.delHandle(props.row)}>
                  删除
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '发票号码',
          dataIndex: 'goldenInvoiceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开票日期',
          dataIndex: 'invoiceDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发票代码',
          dataIndex: 'goldenInvoiceCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '业务类型',
          dataIndex: 'buzinessType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6089'),
          render: props => {
            return <span>{this.createDictText(props.row.buzinessType, '6089')}</span>;
          }
        },
        {
          title: '异常原因',
          dataIndex: 'abnormalReason',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
